package com.example.p2pinvest.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.p2pinvest.R;
import com.example.p2pinvest.util.UIUtils;

/**
 * @author h2tc01
 * @version 1.0.0
 * 2022/8/6
 * 自定义视图
 */
public class RoundProgress extends View {

    //设置绘制的图形基本属性
    /*
    private int roundColor = Color.GRAY;   //圆环的颜色
    private int roundProgressColor = Color.RED; //进度条的颜色
    private int textColor = Color.BLUE;  //文本的颜色
    private int roundWidth = UIUtils.dp2px(10);  //圆环的宽度
    private int textSize = UIUtils.dp2px(20);  //文本字体大小
    private float max = 100;  //圆环的最大值
    private float progress = 50;  //圆环的进度
    */

    private int roundColor;   //圆环的颜色
    private int roundProgressColor; //进度条的颜色
    private int textColor;  //文本的颜色
    private float roundWidth;  //圆环的宽度
    private float textSize;  //文本字体大小
    private int max;  //圆环的最大值
    private int progress;  //圆环的进度

    private Paint paint;  //画笔
    private int width;  //当前视图的宽度 = 高度

    public RoundProgress(Context context) {
        this(context, null);
    }

    public RoundProgress(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RoundProgress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        //初始化画笔
        paint = new Paint();
        paint.setAntiAlias(true);  //抗锯齿

        //获取自定义属性的数据
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgress);
        roundColor = typedArray.getColor(R.styleable.RoundProgress_roundColor, Color.GRAY);
        roundProgressColor = typedArray.getColor(R.styleable.RoundProgress_roundProgressColor, Color.RED);
        textColor = typedArray.getColor(R.styleable.RoundProgress_textColor, Color.GREEN);
        roundWidth = typedArray.getDimension(R.styleable.RoundProgress_roundWidth, UIUtils.dp2px(10));
        textSize = typedArray.getDimension(R.styleable.RoundProgress_textSize, UIUtils.dp2px(20));
        max = typedArray.getInteger(R.styleable.RoundProgress_max, 100);
        progress = typedArray.getInteger(R.styleable.RoundProgress_progress, 30);

        //回收处理
        typedArray.recycle();
    }

    public int getMax() {
        return max;
    }

    public void setMax(int max) {
        this.max = max;
    }

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;
    }

    //测量,获取当前视图的宽高
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = this.getMeasuredWidth();
    }

    /**
     * @param canvas 画布,对应视图在布局中的范围区间
     */
    @Override
    protected void onDraw(Canvas canvas) {
        //1.绘制圆环
        //获取圆心坐标
        int cx = width / 2;
        int cy = width / 2;
        float radius = width / 2 - roundWidth / 2;
        paint.setColor(roundColor);
        paint.setStyle(Paint.Style.STROKE);  //圆环样式
        paint.setStrokeWidth(roundWidth);  //圆环的宽度
        canvas.drawCircle(cx, cy, radius, paint);
        //2.绘制圆弧
        RectF rectF = new RectF(roundWidth / 2, roundWidth / 2, width - roundWidth / 2, width - roundWidth / 2);
        paint.setColor(roundProgressColor);
        canvas.drawArc(rectF, 0, progress * 360 / max, false, paint);
        //3.绘制文本
        String text = progress * 100 / max + "%";
        paint.setColor(textColor);
        paint.setTextSize(textSize);
        paint.setStrokeWidth(0);

        Rect rect = new Rect();
        paint.getTextBounds(text, 0, text.length(), rect);

        int x = width / 2 - rect.width() / 2;
        int y = width / 2 + rect.height() / 2;

        canvas.drawText(text, x, y, paint);

    }
}
